package cn.zyq.ykb.service.config.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.config.ConfigNavigation;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.config.ConfigCpaMapper;
import cn.zyq.ykb.beans.config.ConfigCpa;
import cn.zyq.ykb.service.config.ConfigCpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * 配置-网赚Service业务层处理
 *
 * @author SystemAuth
 * @date 2024-01-11
 */
@Service
public class ConfigCpaServiceImpl  extends BaseService implements ConfigCpaService {

    @Autowired
    private ConfigCpaMapper configCpaMapper;

    /**
     * 查询配置-网赚
     *
     * @param configCpaID 配置-网赚主键
     * @return 配置-网赚
     */
    @Override
    public ConfigCpa selectConfigCpaByConfigCpaID(Integer configCpaID) {
        ConfigCpa configCpa = configCpaMapper.selectByPrimaryKey(configCpaID);
        if (configCpa.getDeleted() == 1) {
            return null;
        }
        return configCpa;
    }

    /**
     * 查询配置-网赚
     *
     * @param serviceID 业务ID
     * @return 配置-网赚
     */
    @Override
    public ConfigCpa selectConfigCpaByServiceID(String serviceID) {
        ConfigCpa param = new ConfigCpa();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.configCpaMapper.selectOne(param);
    }

    /**
     * 查询配置-网赚列表
     *
     * @param pageParam
     * @return 配置-网赚
     */
    @Override
    public PageInfo<ConfigCpa> selectConfigCpaList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ConfigCpa> list = this.configCpaMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询配置-网赚列表
     *
     * @param pageParam
     * @return 配置-网赚
     */
    @Override
    public List<ConfigCpa> selectConfigCpaAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.configCpaMapper.list(pageParam.getKv());
    }
    /**
     * 新增配置-网赚
     *
     * @param param 配置-网赚
     * @return 结果
     */
    @Override
    public int insertConfigCpa(ConfigCpa param) {
        ConfigCpa page = new ConfigCpa();
        page.setDeleted(0);
        ConfigCpa configPage = this.configCpaMapper.selectOne(page);
        if (BeanUtil.isEmpty(configPage)){
            this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
            return this.configCpaMapper.insertSelective(param);
        }else {
            param.setConfigCpaID(configPage.getConfigCpaID());
            this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
            return this.configCpaMapper.updateByPrimaryKeySelective(param);
        }
    }

    /**
     * 修改配置-网赚
     *
     * @param param 配置-网赚
     * @return 结果
     */
    @Override
    public int updateConfigCpa(ConfigCpa param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.configCpaMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除配置-网赚
     *
     * @param configCpaIDs 需要删除的配置-网赚主键
     * @return 结果
     */
    @Override
    public int deleteConfigCpaByConfigCpaIDs(Integer[] configCpaIDs) {
        int i = 0;
        for (Integer configCpaID : configCpaIDs) {
            i += this.deleteConfigCpaByConfigCpaID(configCpaID);
        }
        return i;
    }

    /**
     * 删除配置-网赚信息
     *
     * @param configCpaID 配置-网赚主键
     * @return 结果
     */
    @Override
    public int deleteConfigCpaByConfigCpaID(Integer configCpaID) {
        ConfigCpa configCpa = this.selectConfigCpaByConfigCpaID(configCpaID);
        if (configCpa == null) {
            return 0;
        } else {
            ConfigCpa storeInfo = new ConfigCpa();
            storeInfo.setConfigCpaID(configCpaID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.configCpaMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
